package com.mumu.xiaodu

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class CategoryAdapter(
    private val categories: List<Category>,
    private val onCategoryClick: (Category) -> Unit
) : RecyclerView.Adapter<CategoryAdapter.CategoryViewHolder>() {
    
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoryViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_category, parent, false)
        return CategoryViewHolder(view)
    }
    
    override fun onBindViewHolder(holder: CategoryViewHolder, position: Int) {
        holder.bind(categories[position])
    }
    
    override fun getItemCount(): Int = categories.size
    
    inner class CategoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private val tvCategoryName: TextView = itemView.findViewById(R.id.tvCategoryName)
        
        fun bind(category: Category) {
            tvCategoryName.text = category.name
            
            // 设置选中状态样式
            if (category.isSelected) {
                itemView.setBackgroundResource(R.drawable.category_selected)
                tvCategoryName.setTextColor(itemView.context.getColor(R.color.white))
            } else {
                itemView.setBackgroundResource(R.drawable.category_normal)
                tvCategoryName.setTextColor(itemView.context.getColor(R.color.text_secondary))
            }
            
            itemView.setOnClickListener {
                onCategoryClick(category)
            }
            
            // 设置焦点样式
            itemView.setOnFocusChangeListener { _, hasFocus ->
                if (hasFocus) {
                    if (!category.isSelected) {
                        itemView.setBackgroundResource(R.drawable.category_focused)
                    }
                } else {
                    if (category.isSelected) {
                        itemView.setBackgroundResource(R.drawable.category_selected)
                    } else {
                        itemView.setBackgroundResource(R.drawable.category_normal)
                    }
                }
            }
        }
    }
} 